package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lt.bcra.mapper.BcraGeneTestMapper;
import com.lt.bcra.entity.BcraGeneTest;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.BcraGeneTestService;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 基因检测表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
public class BcraGeneTestServiceImpl extends ServiceImpl<BcraGeneTestMapper, BcraGeneTest> implements BcraGeneTestService {

    @Autowired
    private BcraGeneTestMapper bcraGeneTestMapper;


    @Override
    public ResultData saveModule(List<BcraGeneTest> bcraGeneTestList) {
        int count = 0;

        for (BcraGeneTest bcraGeneTest : bcraGeneTestList) {
            bcraGeneTest.setModuleId(5);
            bcraGeneTest.setModuleTitle("个人及家族病史表");

            //todo 文件上传路径与id

            int ok = bcraGeneTestMapper.insert(bcraGeneTest);
            if (ok != 1) {
                throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
            }
            count++;
        }
        return ResultData.getResult(count);
    }

    @Override
    public ResultData updateModule(List<BcraGeneTest> bcraGeneTestList) {
        String questionnaireCode = bcraGeneTestList.get(0).getQuestionnaireCode();
        QueryWrapper<BcraGeneTest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);

        int count = 0;

        for (BcraGeneTest bcraGeneTest : bcraGeneTestList) {

            //todo 文件上传路径与id

            int ok = bcraGeneTestMapper.update(bcraGeneTest, queryWrapper);
            if (ok != 1) {
                throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
            }
            count++;
        }
        return ResultData.getResult(count);
    }

    @Override
    public ResultData removeByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraGeneTest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        Integer ok = bcraGeneTestMapper.delete(queryWrapper);
        if (ok < 1) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraGeneTest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        List<BcraGeneTest> bcraGeneTestList = bcraGeneTestMapper.selectList(queryWrapper);
        if (bcraGeneTestList == null) {
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraGeneTestList);

    }
}
